package com.sun.json.fast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.sun.json.pojo.Person;
import com.sun.json.pojo.ResultVO;
import org.junit.Test;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class JsonTest {
    @Test
    public void test01(){
        Person person = new Person();
        person.setId(1L);
//        person.setName("损塞");
        person.setPwd("你猜");
        person.setWebsiteUrl("www.baidu.com");
        person.setRegisterDate(new Date());
        person.setBirthDay(LocalDateTime.now());
        person.setAddr("河北河南");
        //序列化
        Object personJson = JSON.toJSON(person);
        System.out.println(personJson);
        System.out.println(JSON.toJSONString(person));
        //对象参数为null 序列化时 将不参与序列化
        System.out.println(JSON.toJSONString(person, SerializerFeature.WriteMapNullValue));//"name":null
        System.out.println(JSON.toJSONString(person, SerializerFeature.WriteNullStringAsEmpty));//"name":""
        System.out.println(JSON.toJSONString(person, SerializerFeature.WriteNullBooleanAsFalse));
    }

    @Test
    public void test02(){
        List<Person> persons = new ArrayList<>();
        Person person = new Person();
        person.setPwd("你猜");
        person.setWebsiteUrl("www.baidu.com");
        persons.add(person);
        persons.add(person);
        persons.add(person);
        //[{"pwd":"你猜","websiteUrl":"www.baidu.com"},{"$ref":"$[0]"},{"$ref":"$[0]"}]
        System.out.println(JSON.toJSONString(persons));
        //禁用引用探测
        System.out.println(JSON.toJSONString(persons,SerializerFeature.DisableCircularReferenceDetect));
    }
    @Test
    public void test03(){
        String str = "[{\"pwd\":\"你猜\",\"websiteUrl\":\"www.baidu.com\"},{\"pwd\":\"你猜\",\"websiteUrl\":\"www.baidu.com\"},{\"pwd\":\"你猜\",\"websiteUrl\":\"www.baidu.com\"}]\n";
        List<Person> list = JSON.parseObject(str, List.class);
        System.out.println(list);
        ResultVO<List<Person>> resultVO = ResultVO.buildSuccess(list);
        String jsonString = JSON.toJSONString(resultVO);
        System.out.println("ResultVO转JSONSTRING" + jsonString);
        //这种方式转换需要强转List<Person>
//        ResultVO parseObject = JSON.parseObject(jsonString, ResultVO.class);
//        Object data = parseObject.getData();
//        System.out.println("JSONSTRING转ResultVO" + parseObject);
        ResultVO<List<Person>> listResultVO = JSON.parseObject(jsonString, new TypeReference<ResultVO<List<Person>>>() {
        });
        List<Person> data = listResultVO.getData();
        System.out.println("JSONSTRING转ResultVO" + listResultVO);
        System.out.println("获得的data" + data);

    }
    @Test
    public void test04() {
        Person person = new Person();
        person.setId(1L);
//        person.setName("损塞");
        person.setPwd("你猜");
        person.setWebsiteUrl("www.baidu.com");
        person.setRegisterDate(new Date());
        person.setBirthDay(LocalDateTime.now());
        person.setAddr("河北河南");
        //序列化  美化输出  一般不用
        /**
         * {
         * 	"addr":"河北河南",
         * 	"birthDay":"2021-10-24",
         * 	"id":1,
         * 	"pwd":"你猜",
         * 	"registerDate":"2021-10-24 17:36:33",
         * 	"websiteUrl":"www.baidu.com"
         * }
         */
        System.out.println(JSON.toJSONString(person,true));
    }

    public static void main(String[] args) {
        String str = "{\"code\": 20000, \"data\": \"http://192.168.30.37:9000/data/TENANT409411205547950080/ROBOT/wxid_46h6flqyxtut22/20210111/details.json?X-Amz- Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=100baotech.com%2F20211029%2Fus-east-1%2Fs3%2Faws4_request&X-Amz- Date=20211029T034748Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz- Signature=e99b95b79d5dcaeb90542ba9deb8e9f88fd1c0331ec5f0a0eca62f72a1e6fa0e\", \"msg\": \"success\", \"success\": true, \"trace\": null}";
        Map map = JSON.parseObject(str, Map.class);
        System.out.println(map);
    }
}
